package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.Behavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;

@Service
public class BehaviorServiceImpl implements BehaviorService {


    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 点赞方法
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {

//        1.校验参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

//        2。设置参数
        Behavior behavior = new Behavior();
        behavior.check();
        behavior.setArticleId(dto.getArticleId());

        Boolean flag = true;
//        判断点赞？取消点赞
        if (dto.getOperation() == 0) {
            behavior.setIslike(true);
        } else if (dto.getOperation() == 1) {
            flag = false;
            behavior.setIslike(false);
        } else {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

//        3.出入redis
//        获取用户id
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userid = user.getId();
        String uid = userid.toString();
        if(userid==0 || userid==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"请登录后访问");
        }
//        4判断用户行为
//        取出用户JSON将用户行为的JSON转Map
        String mapJSon = (String) redisTemplate.opsForValue().get(uid);
        Map articlemap = JSON.parseObject(mapJSon, Map.class);
//        判断用户行为集合是否为空
        if (articlemap == null || articlemap.size() == 0) {
//            为空添加
            articlemap = new HashMap();
            SaveBehavior(dto.getArticleId(), behavior, uid, articlemap);
        }
//        不为空，取出用户对当前文章的行为对象JSON转Behavior对象
        String be = (String) articlemap.get(dto.getArticleId());
        Behavior behavior1 = JSON.parseObject(be, Behavior.class);

//        判断对本文章是否操作
        if (behavior1 == null) {
            SaveBehavior(dto.getArticleId(), behavior, uid, articlemap);
        } else {
            behavior1.setIslike(flag);
            SaveBehavior(dto.getArticleId(), behavior1, uid, articlemap);
        }

//        返回参数
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 不喜欢功能
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlikesBehavior(UnLikesBehaviorDto dto) {
//        1.校验参数
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

//        2。设置参数
        Behavior behavior = new Behavior();
        behavior.check();
        behavior.setArticleId(dto.getArticleId());

        Boolean flag = true;
//        判断点赞？取消点赞
        if (dto.getType() == 0) {
            behavior.setIsunlike(true);
        } else if (dto.getType() == 1) {
            flag = false;
            behavior.setIsunlike(false);
        } else {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

//        3.出入redis
//        获取用户id
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userid = user.getId();
        String uid = userid.toString();
        if(userid==0 || userid==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"请登录后访问");
        }
//        4判断用户行为
//        取出用户JSON将用户行为的JSON转Map
        String mapJSon = (String) redisTemplate.opsForValue().get(uid);
        Map articlemap = JSON.parseObject(mapJSon, Map.class);
//        判断用户行为集合是否为空
        if (articlemap == null || articlemap.size() == 0) {
//            为空添加
            articlemap = new HashMap();
            SaveBehavior(dto.getArticleId(), behavior, uid, articlemap);
        }
//        不为空，取出用户对当前文章的行为对象JSON转Behavior对象
        String be = (String) articlemap.get(dto.getArticleId());
        Behavior behavior1 = JSON.parseObject(be, Behavior.class);

//        判断对本文章是否操作
        if (behavior1 == null) {
            SaveBehavior(dto.getArticleId(), behavior, uid, articlemap);
        } else {
            behavior1.setIsunlike(flag);
            SaveBehavior(dto.getArticleId(), behavior1, uid, articlemap);
        }

//        返回参数
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读次数
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadDto dto) {
        //        1.校验参数
        if (dto == null || dto.getArticleId() == null ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        校验参数
        if (dto.getCount() == null && dto.getCount()!=1) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

//        2。设置参数
        Behavior behavior = new Behavior();
        behavior.check();
        behavior.setArticleId(dto.getArticleId());

//        3.出入redis
//        获取用户id
        ApUser user = AppThreadLocalUtil.getUser();
        Integer userid = user.getId();
        String uid = userid.toString();
        if(userid==0 || userid==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"请登录后访问");
        }
//        4判断用户行为
//        取出用户JSON将用户行为的JSON转Map
        String mapJSon = (String) redisTemplate.opsForValue().get(uid);
        Map articlemap = JSON.parseObject(mapJSon, Map.class);
//        判断用户行为集合是否为空
        if (articlemap == null || articlemap.size() == 0) {
//            为空添加
            articlemap = new HashMap();
            SaveBehavior(dto.getArticleId(), behavior, uid, articlemap);
        }
//        不为空，取出用户对当前文章的行为对象JSON转Behavior对象
        String be = (String) articlemap.get(dto.getArticleId());
        Behavior behavior1 = JSON.parseObject(be, Behavior.class);

//        判断对本文章是否操作
        if (behavior1 == null ) {
            behavior.setCount(1);
            SaveBehavior(dto.getArticleId(), behavior, uid, articlemap);
        } else if (behavior1.getCount()==null){
            behavior1.setCount(1);
            SaveBehavior(dto.getArticleId(), behavior1, uid, articlemap);
        }else {
            behavior1.setCount(behavior1.getCount()+1);
            SaveBehavior(dto.getArticleId(), behavior1, uid, articlemap);
        }
//        返回参数
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 添加behavior到redis
     *
     * @param id
     * @param behavior
     * @param userid
     * @param articlemap
     */
    private void SaveBehavior(Long id, Behavior behavior, String userid, Map articlemap) {
        String behaviorJSON = JSON.toJSONString(behavior);
        articlemap.put(id, behaviorJSON);
        String key = userid.toString();
        String value = JSON.toJSONString(articlemap);
        redisTemplate.opsForValue().set(key, value);
    }
}